package ycl.common.config;

import feign.RequestInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import ycl.common.constants.SecurityConstant;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * 远程调用配置
 *
 * @author ycl
 * @date 2022-03-09 15:23:24
 */
public class NativeFeignConfig {
	@Bean
	public RequestInterceptor requestInterceptor(){
		return requestTemplate -> {
			ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.currentRequestAttributes();
			HttpServletRequest req = servletRequestAttributes.getRequest();
			Map<String, Collection<String>> headerMap = new HashMap<>();
			//获取需要传递的头信息
			String token = SecurityConstant.TOKEN;
			String loginType = SecurityConstant.LOGIN_TYPE;

			String tokens = req.getHeader(token);
			String appId = req.getHeader(loginType);
			headerMap.put(token, Collections.singletonList(tokens));
			headerMap.put(loginType, Collections.singletonList(appId));
			//feign请求时，便可携带上该信息
			requestTemplate.headers(headerMap);
		};
	}
}
